<?php
/**
 * ThemeFile.php
 *
 * @category    Madoqua
 * @package     Controller
 */

/** base class */
require_once 'Zend/Controller/Router/Route/Interface.php';

/** madoqua loader */
require_once 'Madoqua/Loader.php';

/**
 * Madoqua_Controller_Router_Route_ThemeFile
 *
 * @category    Madoqua
 * @package     Controller
 */
class Madoqua_Controller_Router_Route_ThemeFile implements Zend_Controller_Router_Route_Interface 
{
    /**
     * constructor
     */
    public function __construct() 
    {
        Madoqua_Loader::loadModel('Theme_Theme');
                
    }
    
    /**
     * Instantiates route based on passed Zend_Config structure
     *
     * @param Zend_Config $config Configuration object
     */
    public static function getInstance(Zend_Config $config)
    {
        return new self();
    }

    /**
     * match a path to the route
     *
     * @param string $path
     * @return bool|array
     */
    public function match($path) {
        $split = explode('/', trim($path, '/'));
        //split the uri over the path
        
        if (count($split) < 3) {
            return false;
        }
        //count must be at least 3
        
        $data = array();
        //data
        if (!in_array($split[0], Theme_Theme::getNameList())) {
            return false;
        }
        $data['theme'] = array_shift($split);
        //the first item of the array must be the identifier of a theme
        
        if (!is_numeric($split[0])) {
            return false;
        }
        $data['last_modified'] = (int) array_shift($split);
        //second part must be numeric (the last modified bit)
        
        $data['file'] = implode(DIRECTORY_SEPARATOR, $split);
        //the rest is the file

        $data['module'] = 'theme';
        $data['controller'] = 'file';
        $data['action'] = 'serve';
        
        return $data;
    }
    
    /**
     * assemble a route
     *
     * @param array $data
     * @param bool $reset
     * @param bool $encode
     */
    public function assemble($data = array(), $reset = false, $encode = false) {
        if (!isset($data['theme'], $data['file'])) {
            throw new Exception('Theme and File must be set in the data for this route');
        }
        
        $theme = trim($data['theme']);
        $file = trim($data['file'], '/');


        $lastModified = Theme_Theme::getLastModified($theme);
        //get last modified

        return $theme . '/' .$lastModified . '/' . $file;
    }
}